<?php
namespace app\portal\model;

use think\Model;

class House extends Model
{
  protected $name = 'houses';

  public $sellTypes = [
    '1' => '在售',
    '2' => '即售',
    '3' => '预售',
    '4' => '已售',
  ];

  public function profile()
  {
      return $this->hasOne('HouseData', 'house_id', 'id');
  }

  public function getKeywordsAttr($value) {
    return preg_split('/(,|，|\s+)/', $value);
  }

  public function getThumbAttr($value) {
    return cmf_get_image_preview_url($value);
  }

  public function getSellTypeAttr($value) {
    if ($value == 0) {
      $value = 1;
    }

    return [
      'value' => $value,
      'name' => $this->sellTypes[$value]
    ];
  }

  public function favorite() {
    return $this->hasOne('UserFavorite', 'object_id', 'id');
  }

  public function favorites() {
    return $this->hasMany('UserFavorite', 'object_id', 'id');
  }

  public function userPassword() {
    return $this->hasOne('UserHousePasswordModel', 'house_id', 'id');
  }

  public function passwords () {
    return $this->hasMany('HousePassword', 'house_id', 'id');
  }

  public function count() {
    return $this->hasOne('HouseCount', 'house_id', 'id');
  }

}
